package com.graduate.game.topic.controller;

import com.alibaba.fastjson.JSON;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.graduate.game.assignment.pojo.assignment;
import com.graduate.game.topic.pojo.comment;
import com.graduate.game.topic.pojo.topic;
import com.graduate.game.topic.service.TopicService;
import com.graduate.game.user.pojo.User;
import com.graduate.game.user.pojo.userdata;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.sql.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@ResponseBody
@RequestMapping("/topic")
@CrossOrigin(origins = "*", maxAge = 3600)
public class topicController {
    @Autowired
    public TopicService topicService;
    @RequestMapping("/getall")
    public List<topic> getall(HttpServletRequest request, HttpServletResponse response) throws Exception {
        response.setHeader("Access-Control-Allow-Origin","*");
        String s1 = request.getReader().readLine();
        Map maps =(Map) JSON.parse(s1);
        int pageNum=Integer.parseInt(maps.get("currentPage").toString());
        int pagesize=Integer.parseInt(maps.get("pagesize").toString());
        Map<String,Object> data = new HashMap<>();
        data.put("currIndex",(pageNum-1)*pagesize);
        data.put("pageSize",pagesize);
        List<topic> allBypage = topicService.findAllBypage(data);
        return allBypage;
    }
    @RequestMapping("/getallnum")
    public int getallnum(HttpServletRequest request, HttpServletResponse response) throws Exception {
        List<topic> topics = topicService.lookAll();
        System.out.println("topic.size:"+topics.size());
        return topics.size();
    }
    @RequestMapping("/random")
    public String randomOne(HttpServletRequest request, HttpServletResponse response) throws Exception {
        response.setHeader("Access-Control-Allow-Origin","*");
        ObjectMapper objectmapper = new ObjectMapper();
         topic topics = topicService.getrandomOneger();
        String str = objectmapper.writeValueAsString(topics);
        return str;
    }

    //通过ID获取一条内容
    @RequestMapping("/idcontent")
    public topic idcontent(HttpServletRequest request, HttpServletResponse response) throws Exception {
        response.setHeader("Access-Control-Allow-Origin","*");
        String s1 = request.getReader().readLine();
        topic iDone = topicService.getIDone(Integer.parseInt(s1));
        return iDone;
    }
    //发布话题
    @RequestMapping("/publish")
    public String addTopic(HttpServletRequest request, HttpServletResponse response) throws Exception
    {
        response.setHeader("Access-Control-Allow-Origin","*");
        String s1 = request.getReader().readLine();
        System.out.println(s1);
        Map maps =(Map) JSON.parse(s1);
        topic topic = new topic();
        Date date = new Date(System.currentTimeMillis()-5400000);
        topic.setTitle((String) maps.get("title"));
        topic.setContent((String) maps.get("content"));
        topic.setPublisher((String) maps.get("publisher"));
        topic.setTime(date);
        topic.setAnnex(1);
        topicService.insertTopic(topic);
        return "发布成功";
    }
    @RequestMapping("/Onepublisher")
    public List<topic> getOneUser(HttpServletRequest request, HttpServletResponse response) throws Exception
    {
        response.setHeader("Access-Control-Allow-Origin","*");
        String s1 = request.getReader().readLine();
        System.out.println(s1);
        return topicService.getOneUser(s1);
    }
    //模糊查询话题
    @RequestMapping("/serch")
    public List<topic> serchUser(HttpServletRequest request, HttpServletResponse response) throws Exception
    {
        response.setHeader("Access-Control-Allow-Origin","*");
        System.out.println("进入用户模糊查询");
        String s1 = request.getReader().readLine();
        return topicService.serchtopic(s1);
    }

    @RequestMapping("/addAnnex")
    public int addAnnex(HttpServletRequest request, HttpServletResponse response) throws Exception
    {
        response.setHeader("Access-Control-Allow-Origin","*");
        String s1 = request.getReader().readLine();
        System.out.println("进入annex");
        System.out.println(s1);
        int i = topicService.checkAnnext(Integer.parseInt(s1));
        i++;
        System.out.println("i:"+i);
        topic topic = new topic();
        topic.setThemeid(Integer.parseInt(s1));
        topic.setAnnex(i);
        int i1 = topicService.addTopicnum(topic);
        return 1;
    }

    @RequestMapping("/comment")
    public String addComment(HttpServletRequest request, HttpServletResponse response) throws Exception
    {
        response.setHeader("Access-Control-Allow-Origin","*");
        String s1 = request.getReader().readLine();
        Map maps =(Map) JSON.parse(s1);
        comment comment = new comment();
        comment.setThemeid(Integer.parseInt(maps.get("themeid").toString()));
        comment.setCommenter((String) maps.get("commenter"));
        comment.setContent((String) maps.get("content"));
        comment.setPublisher((String) maps.get("publisher"));
        topicService.insertComment(comment);
        System.out.println("评论成功");
        return "发布成功";
    }
    @RequestMapping("/getAllComment")
    public List<comment> getAllComments(HttpServletRequest request, HttpServletResponse response) throws Exception
    {
        response.setHeader("Access-Control-Allow-Origin","*");
        String s1 = request.getReader().readLine();
        List<comment> getallcommnets = topicService.getallcommnets(Integer.parseInt(s1));
        return getallcommnets;
    }
    //根据ID删除一条话题
    @RequestMapping("/delByid")
    public String delByid(HttpServletRequest request, HttpServletResponse response) throws Exception
    {
        response.setHeader("Access-Control-Allow-Origin","*");
        String s1 = request.getReader().readLine();
        System.out.println("话题操作:"+s1);
        topicService.delById(Integer.parseInt(s1));
        return "删除成功";
    }
}
